package com.boom.springapp.javafxdemo.controller;

import com.boom.springapp.javafxdemo.service.MainService;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author hhl
 * @version 1.0
 * @description
 * @date 2025/11/18 23:10
 */
@Component
public class MainController {

    public TabPane tabPane;
    @Autowired
    private MainService mainService; // 注入Spring管理的服务

    @FXML private TextField nameInput;
    @FXML private Label welcomeText;
    @FXML private Button helloButton;
    @FXML private Button goodbyeButton;

    @FXML
    public void initialize() {
        // 设置默认选中第4个标签页（设置页面，索引从0开始）
        tabPane.getSelectionModel().select(3);
    }


    @FXML
    protected void onHelloButtonClick() {
        if (nameInput == null) {
            System.err.println("nameInput is not initialized properly");
            return;
        }
        String name = nameInput.getText();
        String greeting = mainService.getGreeting(name);
        welcomeText.setText(greeting);
    }

    @FXML
    protected void onGoodbyeButtonClick() {
        if (nameInput == null || welcomeText == null) {
            System.err.println("UI components are not initialized properly");
            return;
        }
        String name = nameInput.getText();
        String farewell = mainService.getFarewell(name);
        welcomeText.setText(farewell);
    }
}
